package com.link_with_health.service.impl;

import com.link_with_health.exception.BaseException;
import com.link_with_health.exception.ModifyDatabaseFailedException;
import com.link_with_health.exception.NullQueryResultException;
import com.link_with_health.mapper.WaitingQueueMapper;
import com.link_with_health.model.WaitingQueueEntity;
import com.link_with_health.service.BaseService;
import com.link_with_health.service.WaitingQueueService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("waitingQueueService")
public class WaitingQueueServiceImpl extends BaseService implements WaitingQueueService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    WaitingQueueMapper waitingQueueMapper;

    /**
     * 通过候诊id获取候诊信息
     * @param id 候诊id
     * @return  候诊信息
     * @throws BaseException 异常
     */
    @Override
    public WaitingQueueEntity getWaitingQueueById(Long id) throws BaseException {
        WaitingQueueEntity entity = null;
        try {
            entity = waitingQueueMapper.queryWaitingQueueById(id);
        } catch (Exception e) {
            logger.error("[query waitingQueue failed]",e);
        }
        if (entity == null)
            throw new NullQueryResultException("WAITINGQUEUE.QUEUE_ID: "+id);
        return entity;
    }

    /**
     * 通过医生id获取候诊信息
     * @param id 医生id
     * @return  候诊信息
     * @throws BaseException 异常
     */
    @Override
    public List<WaitingQueueEntity> getWaitingQueueByDoctor(Long id) throws BaseException {
        List<WaitingQueueEntity> waitingQueues = null;
        try {
            waitingQueues = waitingQueueMapper.queryWaitingQueueByDoctor(id);
        } catch (Exception e) {
            logger.error("[query waitingQueue failed]",e);
        }
        if (waitingQueues == null || waitingQueues.isEmpty())
            throw new NullQueryResultException("WAITINGQUEUE.DOCTOR_ID: "+id);
        return waitingQueues;
    }

    /**
     * 通过患者id获取候诊信息
     * @param id 患者id
     * @return  候诊信息
     * @throws BaseException 异常
     */
    @Override
    public List<WaitingQueueEntity> getWaitingQueueByPatient(Long id) throws BaseException {
        List<WaitingQueueEntity> waitingQueues = null;
        try {
            waitingQueues = waitingQueueMapper.queryWaitingQueueByPatient("%#"+id+"#%");
        } catch (Exception e) {
            logger.error("[query waitingQueue failed]",e);
        }
        if (waitingQueues == null || waitingQueues.isEmpty())
            throw new NullQueryResultException("WAITINGQUEUE.PATIENT_QUEUE: "+id);
        return waitingQueues;
    }

    /**
     * 获取所有候诊信息
     * @return 候诊信息
     * @throws BaseException 异常
     */
    @Override
    public List<WaitingQueueEntity> getAllWaitingQueue() throws BaseException {
        List<WaitingQueueEntity> waitingQueues = null;
        try {
            waitingQueues = waitingQueueMapper.queryAllWaitingQueue();
        } catch (Exception e) {
            logger.error("[query waitingQueue failed]",e);
        }
        if (waitingQueues == null || waitingQueues.isEmpty())
            throw new NullQueryResultException("ALL WAITINGQUEUE ");
        return waitingQueues;
    }

    /**
     * 修改候诊信息
     * @param queue 候诊信息
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int changeWaitingQueue(WaitingQueueEntity queue) throws BaseException {
        int result = 0;
        try {
            if(queue.getPatientQueue()==null){
                queue.setPatientQueue("##");
            }

            result = waitingQueueMapper.updateWaitingQueue(queue);
        } catch (Exception e) {
            logger.error("[change waitingQueue failed]");
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("UPDATE WAITINGQUEUE: "+queue);
        return result;
    }

    /**
     * 增加候诊信息
     * @param queue 候诊信息
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int addWaitingQueue(WaitingQueueEntity queue) throws BaseException {
        int result = 0;
        try {
            if(queue.getPatientQueue()==null){
                queue.setPatientQueue("##");
            }
            result = waitingQueueMapper.insertWaitingQueue(queue);
        } catch (Exception e) {
            logger.error("[add waitingQueue failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("INSERT WAITINGQUEUE: "+queue);
        return result;
    }

    /**
     * 删除候诊信息
     * @param id 候诊id
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int deleteWaitingQueue(Long id) throws BaseException{
        int result = 0;
        try {

            result = waitingQueueMapper.deleteWaitingQueue(id);
        } catch (Exception e) {
            logger.error("[delete waitingQueue failed]");
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("DELETE WAITINGQUEUE.QUEUE_ID: "+id);
        return result;
    }



}
